module(...,package.seeall)

local _new={}
local _config={}
_config.layout={
  HorizontalScrollView;
  layout_width="fill";
  layout_height="48dp";
  HorizontalScrollBarEnabled=false;
  id="scrollView";
  {
    LinearLayout;
    layout_width="fill";
    layout_height="fill";
    paddingLeft="12dp";
    paddingRight="12dp";
    id="paddingLayout";
  };
}
_config.item={
  LinearLayout;
  layout_height="fill";
  --layout_height="48dp";
  gravity="center";
  --backgroundColor=背景颜色;
  {
    LinearLayout;
    layout_marginTop="8dp";
    layout_marginBottom="8dp";
    layout_marginLeft="4dp";
    layout_marginRight="4dp";
    layout_height="32dp";
    id="BackgroundLay";
    {
      LinearLayout;
      gravity="center";
      layout_height="fill";
      id="RippleLay";
      {
        TextView;
        layout_marginRight="12dp";
        id="title";
        layout_marginLeft="12dp";
        textSize="14sp";
      };
    };
  };
}



function _new:initParent(view)
  local ids={}
  loadlayout(_config.layout,ids)
  self.scrollView=ids.scrollView
  self.paddingLayout=ids.paddingLayout
  view.addView(ids.scrollView)
  return self
end


function _new:add(name)


  local view={}
  self.views[name]=view
  view.selected=false
  local ids={}
  view.ids=ids
  view.view=loadlayout(_config.item,ids)

  local drawables={}
  view.drawables=drawables
  --drawables.background={}
  drawables.background=ButtonFrame(nil,nil,nil,0,math.dp2int(16))
  --drawables.background.selected=ButtonFrame(nil,math.dp2int(1),强调色,淡色强调,math.dp2int(16))
  drawables.ripple=Ripple(nil,普通波纹)
  drawables.rippleColor={}
  drawables.rippleColor.normal=ColorStateList(int[0].class{int{}},int{淡色强调波纹})
  drawables.rippleColor.selected=ColorStateList(int[0].class{int{}},int{普通波纹})
  local animators={}
  view.animators=animators
  animators.background={}
  animators.background.normal=ObjectAnimator().ofInt(drawables.background, "color", {淡色强调,0})
  .setDuration(200)
  .setEvaluator(ArgbEvaluator())
  animators.background.selected=ObjectAnimator().ofInt(drawables.background, "color", {0,淡色强调})
  .setDuration(200)
  .setEvaluator(ArgbEvaluator())
  animators.textColor={}
  animators.textColor.normal=ObjectAnimator().ofInt(ids.title, "textColor", {强调色,普通文字色})
  .setDuration(200)
  .setEvaluator(ArgbEvaluator())
  animators.textColor.selected=ObjectAnimator().ofInt(ids.title, "textColor", {普通文字色,强调色})
  .setDuration(200)
  .setEvaluator(ArgbEvaluator())


  self.paddingLayout.addView(view.view)

  ids.title.text=name
  ids.title.textColor=普通文字色
  ids.BackgroundLay.background=drawables.background
  ids.RippleLay.background=drawables.ripple
  ids.BackgroundLay.onClick=self._onClick
  return self
end

function _new:switchChip(name)
  for index,content in pairs(self.views) do
    local ids=content.ids
    local drawables=content.drawables
    local animators=content.animators
    if index==name then
      if not(content.selected) then
        content.selected=true
        animators.background.selected.start()
        animators.textColor.selected.start()
        --drawables.background.setStroke(math.dp2int(1), 强调色)
        --ids.BackgroundLay.background=drawables.background.selected
        --ids.RippleLay.background.setColor()--=self.drawables[index].ripple.selected
        --ids.title.textColor=强调色
        drawables.ripple.setColor(drawables.rippleColor.normal)
      end
      --print(content.lay.getLeft())
      local scrollView=self.scrollView
      local scrollViewMeasuredWidth=scrollView.getMeasuredWidth()
      if scrollViewMeasuredWidth==0 then
        task(100,function()
          scrollView.smoothScrollTo(content.view.getLeft()-scrollViewMeasuredWidth/2+content.view.getMeasuredWidth()/2,0)
        end)
       else
        scrollView.smoothScrollTo(content.view.getLeft()-scrollViewMeasuredWidth/2+content.view.getMeasuredWidth()/2,0)
      end
     else
      if content.selected then
        content.selected=false
        animators.background.normal.start()
        animators.textColor.normal.start()
        --drawables.background.setStroke(math.dp2int(1), 淡色普通)
        drawables.ripple.setColor(drawables.rippleColor.selected)
      end
      --ids.BackgroundLay.background=drawables.background.normal
      --ids.RippleLay.background.setColor()--=self.drawables[index].ripple.normal
      ids.title.textColor=普通文字色
      --drawables.ripple.setColor(drawables.rippleColor.selected)
    end
  end
  return self
end

function _new:setOnClick(func)
  self.onClick=func
  return self
end

function _new:remove(name)
  self.paddingLayout.removeView(self.views[name].view)
  return self
end


function __call()
  import "android.animation.ObjectAnimator"
  import "android.animation.ArgbEvaluator"

  local self=table.clone(_new)
  --self.drawables={}
  self.views={}
  self._onClick=function(view)
    local viewTitle=view.getChildAt(0).getChildAt(0).text
    self:switchChip(viewTitle)
    if self.onClick then
      self.onClick(self,viewTitle)
    end
  end
  return self
end